package edu.nyp.locationservicesapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import edu.nyp.locationservicesapp.R;
import edu.nyp.locationservicesapp.application.LauncherModel;
import edu.nyp.locationservicesapp.base.BaseActivity;
import edu.nyp.locationservicesapp.constant.IPreferencesIds;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private EditText mEditNickName;
    private EditText mEditTextPassword;
    private EditText mEditTextPasswordConfirm;
    private ImageView mPasswordHide, mPasswordConfirmHide;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        mEditNickName = (EditText) findViewById(R.id.et_user_nick);
        mEditTextPassword = (EditText) findViewById(R.id.et_password);
        mEditTextPasswordConfirm = (EditText) findViewById(R.id.et_password_confirm);
        mPasswordHide = (ImageView) findViewById(R.id.set_password_hide_img);
        mPasswordConfirmHide = (ImageView) findViewById(R.id.set_password_confirm_hide_img);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_password_hide_img:
                if (mEditTextPassword.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) {
                    mEditTextPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mPasswordHide.setImageResource(R.mipmap.login_pass_hide);
                } else {
                    mEditTextPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mPasswordHide.setImageResource(R.mipmap.login_pass_see);
                }
                mEditTextPassword.setSelection(mEditTextPassword.getText().length());
                break;
            case R.id.set_password_confirm_hide_img:
                if (mEditTextPasswordConfirm.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) {
                    mEditTextPasswordConfirm.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mPasswordConfirmHide.setImageResource(R.mipmap.login_pass_hide);
                } else {
                    mEditTextPasswordConfirm.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mPasswordConfirmHide.setImageResource(R.mipmap.login_pass_see);
                }
                mEditTextPasswordConfirm.setSelection(mEditTextPasswordConfirm.getText().length());
                break;
            case R.id.btn_register:
                onSet();
                break;
        }
    }

    private void onSet() {
        String userName = mEditNickName.getText().toString().trim();
        if(TextUtils.isEmpty(userName)){
            Toast.makeText(this, this.getString(R.string.login_hint_phone), Toast.LENGTH_SHORT).show();
            return;
        }

        String password = mEditTextPassword.getText().toString().trim();
        String passwordConfirm = mEditTextPasswordConfirm.getText().toString().trim();
        if(TextUtils.isEmpty(password)){
            Toast.makeText(this, this.getString(R.string.password_empty), Toast.LENGTH_SHORT).show();
            return;
        }
        if(!password.equals(passwordConfirm)){
            Toast.makeText(this, this.getString(R.string.password_not_equal), Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(this, this.getString(R.string.reg_success), Toast.LENGTH_SHORT).show();
        LauncherModel.getInstance().getSPManager().commitString(IPreferencesIds.USER_LOGIN_PHONE, userName);
        LauncherModel.getInstance().getSPManager().commitString(userName, password);
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.activity_in_from_right, R.anim.activity_out_from_left);
        finish();
    }
}
